﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Net.Mail;
using System.Configuration;

namespace EARS.App_Code
{
    public class Account
    {
        private string userID;
        private string password;
        private string name;
        private string course;
        private string email;
        private int contactNo;
        private string yearOfStudy;
        private string matricNo;
        private string role;

        public string UserID
        {
            get { return userID; }
            set { userID = value; }
        }

        public string Password
        {
            get { return password; }
            set { password = value; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Course
        {
            get { return course; }
            set { course = value; }
        }
        
        public string Email
        {
            get { return email; }
            set { email = value; }
        }
        
        public int ContactNo
        {
            get { return contactNo; }
            set { contactNo = value; }
        }
        
        public string YearOfStudy
        {
            get { return yearOfStudy; }
            set { yearOfStudy = value; }
        }

        public string MatricNo
        {
            get { return matricNo; }
            set { matricNo = value; }
        }

        public string Role
        {
            get { return role; }
            set { role = value; }
        }

        public Account()
        {
        }

        public Account(string userID, string password, string name, string matricNo, string email, int contactNo, string course)
        {
            this.userID = userID;
            this.password = password;
            this.name = name;
            this.matricNo = matricNo;
            this.email = email;
            this.contactNo = contactNo;
            this.course = course;

            MatricNoDBDataClass mdc = new MatricNoDBDataClass();
            string year = mdc.GetYear(matricNo);
            if (year.Equals("Lecturer"))
            {
                this.role = "Lecturer";
                this.yearOfStudy = "nil";
            }
            else
            {
                this.role = "Student";
                this.yearOfStudy = year;
            }
        }

        public Account(string userID, string password, string name, string matricNo, string email, int contactNo, string role, string course, string yearOfStudy)
        {
            this.userID = userID;
            this.password = password;
            this.name = name;
            this.course = course;
            this.email = email;
            this.contactNo = contactNo;
            this.yearOfStudy = yearOfStudy;
            this.matricNo = matricNo;
            this.role = role;
        }

        public string GetNameByID(string userID)
        {
            AccountDataClass adc = new AccountDataClass();
            string name = adc.GetNameByID(userID);
            return name;
        }

        public int GetContactNoByID(string userID)
        {
            AccountDataClass adc = new AccountDataClass();
            int contactNo = adc.GetContactNoByID(userID);
            return contactNo;
        }

        public string GetEmailByID(string userID)
        {
            AccountDataClass adc = new AccountDataClass();
            string contactNo = adc.GetEmailByID(userID);
            return contactNo;
        }

        public bool Login(string userID, string password)
        {
            AccountDataClass adc = new AccountDataClass();
            bool check = false;
            string Password = adc.RetrievePassword(userID);
            if (Password != null)
            {
                if (password.Equals(Password))
                {
                    check = true;
                }
            }
            return check;
        }

        public bool ForgetPassword(string userID, string email)
        {
            bool check = false;
            AccountDataClass adc = new AccountDataClass();
            string email1 = adc.ForgetPassword(userID);
            if (email1.Equals(email))
            {
                string name = adc.RetrieveName(userID);
                string password = adc.RetrievePassword(userID);

                MailMessage mail = new MailMessage();
                mail.To.Add(email);
                mail.From = new MailAddress(ConfigurationManager.AppSettings["Email"]);
                mail.Subject = "EARS Password Retrival";
                mail.Body = "Hello, " + name + ". Your previous password is: " + password + ".";
                mail.IsBodyHtml = true;
                mail.Priority = MailPriority.High;

                SmtpClient client = new SmtpClient();

                client.UseDefaultCredentials = false;
                client.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["Email"], ConfigurationManager.AppSettings["Password"]);

                client.Port = 587;
                client.Host = "smtp.gmail.com";
                client.EnableSsl = true;


                try
                {
                    client.Send(mail);
                    check = true;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return check;
        }

        public void CreateAcc(Account a)
        {
            AccountDataClass adc = new AccountDataClass();
            adc.CreateAccount(a);
        }

        public bool ChangePassword(string userID, string oldPassword, string newPassword)
        {
            AccountDataClass adc = new AccountDataClass();
            bool check = false;
            string password = adc.RetrievePassword(userID);
            if (oldPassword.Equals(password))
            {
                adc.ChangePassword(userID, newPassword);
                check = true;
            }
            return check;
        }

        public bool CheckMatric(string matricNo)
        {
            bool check = false;
            AccountDataClass adc = new AccountDataClass();
            string num = adc.GetMatric(matricNo);
            if (num != null)
            {
                check = true;
            }
            return check;
        }
    }
}